/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-7-5         admin
 */

package com.tianci.appstore.ui.search;

import java.util.ArrayList;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.search.SearchKeyItemView.OnItemClickListener;
import com.tianci.appstore.utils.SkyConstants;

/**
 * @author admin
 * @ClassName SearchLayout
 * @date 2014-7-5
 */
public class SearchNomalKeyLayout extends SearchInputLayout implements OnItemClickListener {

    private Context mContext;
    private Integer[] keyIDs = {R.string.search_key_a, R.string.search_key_b,
            R.string.search_key_c, R.string.search_key_d, R.string.search_key_e,
            R.string.search_key_f, R.string.search_key_g, R.string.search_key_h,
            R.string.search_key_i, R.string.search_key_j, R.string.search_key_k,
            R.string.search_key_l, R.string.search_key_m, R.string.search_key_n,
            R.string.search_key_o, R.string.search_key_p, R.string.search_key_q,
            R.string.search_key_r, R.string.search_key_s, R.string.search_key_t,
            R.string.search_key_u, R.string.search_key_v, R.string.search_key_w,
            R.string.search_key_x, R.string.search_key_y, R.string.search_key_z,
            R.string.search_key_0, R.string.search_key_1, R.string.search_key_2,
            R.string.search_key_3, R.string.search_key_4, R.string.search_key_5,
            R.string.search_key_6, R.string.search_key_7, R.string.search_key_8,
            R.string.search_key_9};
    private ArrayList<SearchKeyItemView> keyItems;

    private LinearLayout deletLayout, clearLayout;
    private ImageView deletIconView, clearIconView;
    private TextView deletTextView, clearTextView;
    private Button mT9Btn, mNormalBtn;

    private OnNomalInputListener inpListener;

    public interface OnNomalInputListener {
        public void inputEvent_nomal(String str);

        public void deletEvent_nomal();

        public void clearEvent_nomal();
    }

    public SearchNomalKeyLayout(Context context) {
        super(context);
        mContext = context;
    }

    public void setOnNomalInputListener(OnNomalInputListener listener) {
        inpListener = listener;
    }

    public void setButtons(Button t9, Button normal) {
        this.mT9Btn = t9;
        this.mNormalBtn = normal;
    }

    private void initBtnView() {
        deletLayout = new LinearLayout(mContext);
        deletLayout.setFocusable(true);
        deletLayout.setTag("delete");
        setFocusChangeEvent(deletLayout);
        deletLayout.setOnClickListener(btnClickListener);
        deletLayout.setOnKeyListener(btnOkeyListener);
        deletLayout.setGravity(Gravity.CENTER);
        deletLayout.setOrientation(LinearLayout.HORIZONTAL);
        deletLayout.setBackgroundResource(mItemBackId);
        clearLayout = new LinearLayout(mContext);
        clearLayout.setFocusable(true);
        setFocusChangeEvent(clearLayout);
        clearLayout.setTag("clear");
        clearLayout.setOnClickListener(btnClickListener);
        clearLayout.setOnKeyListener(btnOkeyListener);
        clearLayout.setGravity(Gravity.CENTER);
        clearLayout.setOrientation(LinearLayout.HORIZONTAL);
        clearLayout.setBackgroundResource(mItemBackId);

        deletIconView = new ImageView(mContext);
        deletIconView.setImageResource(R.drawable.search_delete);
        clearIconView = new ImageView(mContext);
        clearIconView.setImageResource(R.drawable.search_clear);

        deletTextView = new TextView(mContext);
        deletTextView.setText(R.string.search_delete);
        deletTextView.setTextColor(0xfffffece);
        deletTextView.setTextSize(SkyConstants.Dpi(40));
        clearTextView = new TextView(mContext);
        clearTextView.setText(R.string.search_clear);
        clearTextView.setTextColor(0xfffffece);
        clearTextView.setTextSize(SkyConstants.Dpi(40));

        deletLayout.addView(deletIconView);
        LinearLayout.LayoutParams de_p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        de_p.leftMargin = SkyConstants.Div(16);
        deletLayout.addView(deletTextView, de_p);
        clearLayout.addView(clearIconView);
        clearLayout.addView(clearTextView);
    }

    public void initView() {
        metroKeyLayout = new SearchKeyLayout(mContext);
        initBtnView();
        FrameLayout.LayoutParams del_p = new FrameLayout.LayoutParams(
                SkyConstants.Div(289), SkyConstants.Div(180));
        del_p.leftMargin = SkyConstants.Div(94 - 40);
        this.addView(deletLayout, del_p);
        FrameLayout.LayoutParams cle_p = new FrameLayout.LayoutParams(
                SkyConstants.Div(289), SkyConstants.Div(180));
        cle_p.leftMargin = SkyConstants.Div(462);
        this.addView(clearLayout, cle_p);
        FrameLayout.LayoutParams key_p = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        key_p.leftMargin = SkyConstants.Div(94 - 40);
        key_p.topMargin = SkyConstants.Div(114);
        this.addView(metroKeyLayout, key_p);
        setKeyLayout();
    }

    public void setDeleteLayoutFocus() {
        deletLayout.requestFocus();
    }

    public void setClearLayoutFocus() {
        clearLayout.requestFocus();
    }

    OnClickListener btnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getTag().equals("delete")) {
                if (inpListener != null)
                    inpListener.deletEvent_nomal();
            } else if (v.getTag().equals("clear")) {
                if (inpListener != null)
                    inpListener.clearEvent_nomal();
            }
        }
    };

    private void setKeyLayout() {
        int size = keyIDs.length;
        keyItems = new ArrayList<SearchKeyItemView>();
        metroKeyLayout.setSize(6, 2);
        for (int i = 0; i < size; i++) {
            SearchKeyItemView keyItem = new SearchKeyItemView(mContext, -1,
                    SearchKeyItemView.NOT_T9, SkyConstants.Div(107),
                    SkyConstants.Div(93));
            setFocusChangeEvent(keyItem, keyItem.getOnFocusChangeListener());
            keyItem.setBackgroundResource(mItemBackId);
            keyItem.setOnItemClickListener(this);
            if (i < 6) {
                metroKeyLayout.addSubView(keyItem, 0, i);
            } else if (i >= 6 && i < 12) {
                metroKeyLayout.addSubView(keyItem, 1, i - 6);
            } else if (i >= 12 && i < 18) {
                metroKeyLayout.addSubView(keyItem, 2, i - 12);
            } else if (i >= 18 && i < 24) {
                metroKeyLayout.addSubView(keyItem, 3, i - 18);
            } else if (i >= 24 && i < 30) {
                metroKeyLayout.addSubView(keyItem, 4, i - 24);
            } else if (i >= 30 && i < 36) {
                metroKeyLayout.addSubView(keyItem, 5, i - 30);
            }
            keyItems.add(keyItem);
            keyItem.setTextValue(keyIDs[i]);
            keyItem.setOnKeyListener(itemOnKeyListener);
        }
    }

    OnKeyListener btnOkeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (v.getTag().equals("delete")) {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT
                            || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                        clearLayout.requestFocus();
                        return true;
                    }
                    if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        mT9Btn.requestFocus();
                        return true;
                    }
                } else if (v.getTag().equals("clear")) {
                    // 按键焦点处理
                    if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        mNormalBtn.requestFocus();
                        return true;
                    } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                        deletLayout.requestFocus();
                        return true;
                    } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                        if (mClearFocusListener != null) {
                            return mClearFocusListener.clearFocus(keyCode);
                        }
                    }
                }
            }
            return false;
        }
    };

    OnKeyListener itemOnKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            int id = v.getId();
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    // case KeyEvent.KEYCODE_DPAD_UP:
                    // case KeyEvent.KEYCODE_DPAD_DOWN:
                    // return true;

                    case KeyEvent.KEYCODE_DPAD_LEFT:

                        if (id == 0 || id == 10 || id == 20 || id == 30 || id == 40 || id == 50) {
                            metroKeyLayout.setFocusItem(id + 5);
                            return true;
                        }
                        return false;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        if (mClearFocusListener != null && (id == 5 || id == 15 || id == 25 || id == 35 || id == 45 || id == 55)) {
                            return mClearFocusListener.clearFocus(keyCode);
                        }
                        break;
                    default:
                        break;
                }
            }
            return false;
        }
    };

    @Override
    public void itemClickEvent(String keyValue) {
        if (inpListener != null)
            inpListener.inputEvent_nomal(keyValue);
    }

    @Override
    public void t9_itemClickEvent(SearchKeyItemView view) {
    }

    @Override
    public void onItemFocusEvent(SearchKeyItemView view) {
    }

}
